﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DemoVideoFrameSkelton
{
    using Microsoft.Research.Kinect;
    using Microsoft.Research.Kinect.Audio;
    using Microsoft.Research.Kinect.Nui;

    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        Runtime runtime;
        public MainWindow()
        {
            InitializeComponent();
            runtime = new Runtime();
            runtime.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);
            runtime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
            runtime.VideoFrameReady += new EventHandler<ImageFrameReadyEventArgs>(runtime_VideoFrameReady);
            runtime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(runtime_SkeletonFrameReady);
        }

        void runtime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            var frame = e.SkeletonFrame;
            foreach (var data in frame.Skeletons)
            {
                if (data.TrackingState == SkeletonTrackingState.Tracked)
                {
                    double lx = data.Joints[JointID.HandLeft].Position.X;
                    double ly = data.Joints[JointID.HandLeft].Position.Y;
                    double lz = data.Joints[JointID.HandLeft].Position.Z;
                    double lw = data.Joints[JointID.HandLeft].Position.W;
                    tbLeftHand.Text = String.Format("Left:X={0},Y={1},Z={2},W={3}", lx, ly, lz, lw);
                    double rx = data.Joints[JointID.HandRight].Position.X;
                    double ry = data.Joints[JointID.HandRight].Position.Y;
                    double rz = data.Joints[JointID.HandRight].Position.Z;
                    double rw = data.Joints[JointID.HandRight].Position.W;
                    tbRightHand.Text = String.Format("Right:X={0},Y={1},Z={2},W={3}", rx, ry, rz, rw);
                }
            }
        }

        void runtime_VideoFrameReady(object sender, ImageFrameReadyEventArgs e)
        {
            PlanarImage image = e.ImageFrame.Image;
            VideoFrameImage.Source = BitmapImage.Create(image.Width, image.Height
                , 96, 96, PixelFormats.Bgr32, null, image.Bits, image.Width * image.BytesPerPixel);
        }
    }
}
